package com.example.webdemo.service.impl;

import com.example.webdemo.common.ApiResult;
import com.example.webdemo.common.ResultCodeEnum;
import com.example.webdemo.dao.ImageRecordMapper;
import com.example.webdemo.model.ArticleInfo;
import com.example.webdemo.model.ImageRecord;
import com.example.webdemo.service.ImageRecordService;
import com.example.webdemo.utils.MyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zp
 * @version 1.0
 * @description: ImageRecordService
 * @date 2023/8/16 19:36
 */
@Service
public class ImageRecordServiceImpl implements ImageRecordService {
    @Resource
    private ImageRecordMapper imageRecordMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return imageRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ImageRecord row) {
        return imageRecordMapper.insert(row);
    }

    @Override
    public int insertSelective(ImageRecord row) {
        return imageRecordMapper.insertSelective(row);
    }

    @Override
    public ImageRecord selectByPrimaryKey(Integer id) {
        return imageRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ImageRecord row) {
        return imageRecordMapper.updateByPrimaryKeySelective(row);
    }

    @Override
    public int updateByPrimaryKey(ImageRecord row) {
        return imageRecordMapper.updateByPrimaryKey(row);
    }

    @Override
    public List<ImageRecord> selectByUserId(Integer userId) {
        return imageRecordMapper.selectByUserId(userId);
    }

    @Override
    public ImageRecord selectByImageUrlAndUser(String imageName, Integer userId) {
        return imageRecordMapper.selectByImageUrlAndUser(imageName,userId);
    }

    @Override
    public boolean deleteImageByType(ArticleInfo articleInfo) {
        List<ImageRecord> imageRecords = imageRecordMapper.selectByUserIdAndImageType(articleInfo.getUserId(), 0);
        for (ImageRecord imageRecord: imageRecords) {
            MyUtils.deleteFile(imageRecord.getImageUrl());
        }
        int imageRecordRow = imageRecordMapper.deleteByImageType(articleInfo.getUserId(), 0);
        return imageRecordRow>0;
    }
}
